package com.thug.managers;

import java.util.ArrayList;

import javax.microedition.khronos.opengles.GL10;

import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.camera.hud.controls.AnalogOnScreenControl;
import org.anddev.andengine.engine.camera.hud.controls.AnalogOnScreenControl.IAnalogOnScreenControlListener;
import org.anddev.andengine.engine.camera.hud.controls.BaseOnScreenControl;
import org.anddev.andengine.entity.primitive.Rectangle;
import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.extension.physics.box2d.PhysicsFactory;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;

import android.content.Context;

import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.thug.engine.BaseGameEntity;
import com.thug.engine.EntityManager;
import com.thug.engine.SceneManager;
import com.thug.entities.Player;
import com.thug.entities.Sounder;

public class PlayerManager extends EntityManager<Player> {
    
    //
    Player mPlayer;
    ArrayList<Sounder> mSounders;
    Camera mCamera;
    private AnalogOnScreenControl mOnScreenControl;

	public PlayerManager(SceneManager s) {
		super(s);
		mCamera = s.getCamera();
		mSounders = new ArrayList<Sounder>();
	}

	@Override
	public void onLoad() {
		// create controllers on the screen
		createOnScreenControls();
        // create player sprite and body
        createPlayer();
        mPlayer.setSpeed(2);
        mPlayer.init();
		
	}

	@Override
	public void update(float elapsedTime) {
		mPlayer.update(elapsedTime);
		updateSounders(elapsedTime);
		updateCamera(elapsedTime);		
	}


	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		
	}
	
	public BaseGameEntity getPlayer() {
		return mPlayer;
	}

	
	private void updateSounders(float elapsedTime) {
		for(int i = 0; i < mSounders.size(); i++) {
			Sounder snd = mSounders.get(i);
			snd.update(elapsedTime);
		}
	}
	
	private void updateCamera(float elapsedTime) {
		float entPosX = mPlayer.getSprite().getX();
		float entPosY = mPlayer.getSprite().getY();
		float camPosX = mCamera.getCenterX();
		float camPosY = mCamera.getCenterY();
		float posX = camPosX;
		float posY = camPosY;
		
		if(entPosX > camPosX + 160) {
			posX = entPosX - 160;
		}
		else if(entPosX < camPosX - 160) {
			posX = entPosX + 160;
		}
		
		if(entPosY > camPosY + 106) {
			posY = entPosY - 106;
		}
		else if(entPosY < camPosY - 106) {
			posY = entPosY + 106;
		}
		
		mCamera.setCenter(posX, posY);
	}
	
	/**
	 * 
	 */
	private void createPlayer() {
	    
	    TiledTextureRegion mPlayerTextureRegion = (TiledTextureRegion) getSceneMgr().getResource("player_region");
	    AnimatedSprite s = new AnimatedSprite(940, 450, mPlayerTextureRegion);
	    //s.setScale(0.8f);
	    
	    //
	    final FixtureDef def = PhysicsFactory.createFixtureDef(1, 0.5f, 0.5f, false);
		Body b = PhysicsFactory.createCircleBody(getSceneMgr().getPhysicsWorld(), s, BodyType.DynamicBody, def);
		b.setFixedRotation(true);
		
		mPlayer = new Player(s, b);
		mPlayer.attach(getSceneMgr().getScene());
		b.setUserData(mPlayer);

	}
	
	/**
	 * 
	 */
	private void createOnScreenControls() {
		
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
		Context c = getSceneMgr().getApplicationContext();
		
		BitmapTextureAtlas mOnScreenControlTexture = new BitmapTextureAtlas(512, 512, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		TextureRegion mOnScreenControlBaseTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mOnScreenControlTexture, c, "onscreen_control_base.png", 0, 0);
		TextureRegion mOnScreenControlKnobTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mOnScreenControlTexture, c, "onscreen_control_knob.png", 129, 0);
        getSceneMgr().getEngine().getTextureManager().loadTexture(mOnScreenControlTexture);
        
        
		final AnalogOnScreenControl analogOnScreenControl = new AnalogOnScreenControl(
				0, 
				getSceneMgr().getScreenHeight()*2 - mOnScreenControlBaseTextureRegion.getHeight(),
				getSceneMgr().getCamera(), 
				mOnScreenControlBaseTextureRegion,
				mOnScreenControlKnobTextureRegion, 
				0.1f,
				new IAnalogOnScreenControlListener() {

					@Override
					public void onControlChange(
							BaseOnScreenControl pBaseOnScreenControl,
							float pValueX, float pValueY) {
						mPlayer.move(pValueX, pValueY);
					}

					@Override
					public void onControlClick(
							AnalogOnScreenControl pAnalogOnScreenControl) {						
						
					}
				});
		analogOnScreenControl.getControlBase().setBlendFunction(
				GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		analogOnScreenControl.getControlBase().setAlpha(0.5f);
		// analogOnScreenControl.getControlBase().setScaleCenter(0, 128);
		// analogOnScreenControl.getControlBase().setScale(0.75f);
		// analogOnScreenControl.getControlKnob().setScale(0.75f);
		analogOnScreenControl.refreshControlKnobPosition();

		getSceneMgr().getScene().setChildScene(analogOnScreenControl);
		mOnScreenControl = analogOnScreenControl;
	}
	
	
	
	public void throwSounder() {
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
		Context c = getSceneMgr().getApplicationContext();
		
		BitmapTextureAtlas mSounderTexture;
	    TiledTextureRegion mSounderTextureRegion;
	    
	    mSounderTexture = new BitmapTextureAtlas(32, 32, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
	    mSounderTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mSounderTexture, c, "deto.png", 0, 0, 1, 1);
	    getSceneMgr().getEngine().getTextureManager().loadTexture(mSounderTexture);
	    
	    AnimatedSprite conSprite = new AnimatedSprite(0, 0, mSounderTextureRegion);
	    Rectangle bodySprite = new Rectangle(0, 0, 26, 26);
	    
	    //
	    final FixtureDef sounderBodydef = PhysicsFactory.createFixtureDef(1, 0.5f, 0.5f);
	    final FixtureDef sounderConedef = PhysicsFactory.createFixtureDef(1, 0.5f, 0.5f, true);
		
	    Body sounderBody = PhysicsFactory.createCircleBody(getSceneMgr().getPhysicsWorld(), bodySprite, BodyType.DynamicBody, sounderBodydef);
	    Body sounderConBody = PhysicsFactory.createCircleBody(getSceneMgr().getPhysicsWorld(), conSprite, BodyType.DynamicBody, sounderConedef);
		
	    Sounder sounder = new Sounder(conSprite, sounderBody, sounderConBody);
	    sounder.attach(getSceneMgr().getScene());
	    sounderBody.setUserData(sounder);
	    sounderConBody.setUserData(sounder);
	    
	    sounder.throwSounder(mPlayer.getPostion(), mPlayer.getDirection(), 1);
	    mSounders.add(sounder);
	}

	public AnalogOnScreenControl getOnScreenControl() {
		return mOnScreenControl;
	}

}
